/*=============================================================================

  Manager Event Handler MODULE

  FILE: SYSmgr_EHandler.cpp
  
  DESCRIPTION

    Manager Event Handler Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include "SYSmgr.h"

/*=============================================================================
  XBASE CLASS IMPLEMENTATION
=============================================================================*/
dword SYSmgr::Event_Handler(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	dword		evtID, event;
		
	event = HDEV_GETEVENT(dwEvent);
	evtID = HDEV_GETDEV_ID(dwEvent);

	// Pre-Process the event
	if(Event_PreProc(dwType, dwEvent, wParam, lParam))
	{
		return	TRUE;
	}

	// Block the internal event (Command, State event)
	if(IS_CMD_EVENT(dwType) || IS_STATE_EVENT(dwType))
	{
		return	TRUE;
	}

	return	Event_PostProc(dwType, dwEvent, wParam, lParam);
}

// Hardware Event Handler

dword SYSmgr::HEvent_Handler(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	dword		evtID, event;
	boolean		bDirectAccess = FALSE;

	event = HDEV_GETEVENT(dwEvent);
	evtID = HDEV_GETDEV_ID(dwEvent);

	switch(dwType)
	{
	case AREA_MOD:
	{
		return	TRUE;
	}
	case HFG_DEV:
	{
		EVT_FG(dwEvent, wParam, lParam);
		return	TRUE;
	}
	case IO_MGR:
	{
		EVT_IOmgr(dwEvent, wParam, lParam);
		break;
	}
	}
	
	// Send to State Machine
	if(bDirectAccess == FALSE)
	{
		State_SendEvent(dwType, dwEvent, wParam, lParam);
	}
	else
	{
		// WARNING !! - MUST HADNLE CAREFULLY
		Event_Handler(dwType, dwEvent, wParam, lParam);
	}

	return	TRUE;
}

dword SYSmgr::Event_PreProc(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	dword		evtID, event;
	dword		mj_sts, mn_sts;

	event = HDEV_GETEVENT(dwEvent);
	evtID = HDEV_GETDEV_ID(dwEvent);

	if(IS_CMD_EVENT(dwType) == TRUE)
	{
		if(Command_Handler(dwEvent, wParam, lParam) == TRUE)
			return	TRUE;
	}
	if(IS_STATE_EVENT(dwType) == TRUE)
	{
       	MON_STATE();
	}
	
	switch(dwType)
	{
	case SYS_MGR:
	{
		switch(event)
		{
		case INIT_DONE_E:
		{
			ObjList_Set(HIO_DEV, m_pIO);
			ObjList_Set(HAIO_DEV, m_pAIO);
			ObjList_Set(HMOT_DEV, m_pMot);
			ObjList_Set(AREA_MOD, m_areaMod);
			ObjList_Set(HFG_DEV, m_pFg);
			ObjList_Set(HCAM_DEV, m_pCam);
			break;
		}
		case CLOSE_DONE_E:
		{
			ObjList_Set(HIO_DEV, NULL);
			ObjList_Set(HAIO_DEV, NULL);
			ObjList_Set(HMOT_DEV, NULL);
			ObjList_Set(AREA_MOD, NULL);
			ObjList_Set(HFG_DEV, NULL);
			ObjList_Set(HCAM_DEV, NULL);
			break;
		}
		case SYSPREP_UPDATE_E:
		{
			return	FALSE;
		}
		case ERR_DEVINIT_E:
		{
			break;
		}
		case CLOSE_START_E:
		{
			break;
		}
		case CHANGE_MAJSTATE_E:
		{
			m_mjPrev = lParam;
			
			mj_sts = State_MajCurrent();
			mn_sts = State_MinCurrent();

			//SIGT_STATE(mj_sts, mn_sts);
			return	FALSE;
		}
		case CHANGE_MINSTATE_E:
		{
			m_mnPrev[m_mjPrev] = lParam;
			
			mj_sts = State_MajCurrent();
			mn_sts = State_MinCurrent();

			//SIGT_STATE(mj_sts, mn_sts);
			return	FALSE;
		}
		break;
		}
		// Don't the Custom event to send to State machine
		if(event >= CUSTOM_BASE_E)
		{
			return	FALSE;
		}
		break;
	}
	}

	//-----------------------------------------------------
	// State Machine
	//-----------------------------------------------------
	return	STATE_MACHINE(dwType, dwEvent, wParam, lParam);
}
 
dword SYSmgr::Event_PostProc(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	dword		evtID, event;

	event = HDEV_GETEVENT(dwEvent);
	evtID = HDEV_GETDEV_ID(dwEvent);

	Event_Pass(dwType, dwEvent, wParam, lParam);

	return	TRUE;
}
/*--- END OF SYSmgr_EHandler.CPP ---*/
